//Vuex状态管理

import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
//不同参数的url共同数据也需要先声明
const commonMapCache = {
  state: () => ({

  }),
  getters: {

  },
  mutations: {
    pushCommonData(state, params) {

      if (params.group) {
        if (!state[params.group]) {
          Vue.set(state, params.group, {})
        }
        Vue.set(state[params.group], params.key, params.data)
      } else {
        Vue.set(state, params.key, params.data)
      }
    },

  },
  actions: {

    refreshCommonData({ commit, state }, params) {
      if (params.group) {
        if (params.key) {
          Vue.prototype.$ajax({
            url: params.group,
            method: 'post',
            data: Vue.prototype.$queryObj(params.key),
            success: (res) => {
              commit('pushCommonData', {
                group: params.group,
                key: params.key,
                data: res.data.rows
              })
            },
            failure: () => {
              commit('pushCommonData', {
                group: params.group,
                key: params.key,
                data: []
              })
            },
          })
        } else {
          _.keys(state[params.group]).forEach(key => {
            Vue.prototype.$ajax({
              url: params.group,
              method: 'post',
              data: Vue.prototype.$queryObj(key),
              success: (res) => {
                commit('pushCommonData', {
                  group: params.group,
                  key: key,
                  data: res.data.rows
                })
              },
              failure: () => {
                commit('pushCommonData', {
                  group: params.group,
                  key: key,
                  data: []
                })
              },
            })
          })
        }

      } else {
        Vue.prototype.$ajax({
          url: params.key,
          method: 'post',
          success: (res) => {
            commit('pushCommonData', {
              key: params.key,
              data: res.data.rows
            })
          },
          failure: () => {
            commit('pushCommonData', {
              key: params.key,
              data: []
            })
          },
        })
      }


    }
  },
}


const modifyLayout = localStorage.getItem('modifyLayout') || 'top'
const store = new Vuex.Store({
  modules: {
    commonMapCache,
  },
  state: {
    i18nlocale: 'zh',
    modifyLayout,
    stopRoute: false,
    prevRoute: {},
    allWarehouseMap: {},
    allWarehouseList: [],

    message_notify_unread: 0,
    message_notify_list: [],
  },
  getters: {
    destinationWarehouseList: state => {
      return state.allWarehouseList ? state.allWarehouseList.filter(it => ["DELIVERY", "COMBINATION"].includes(it.type)) : []
    },
    transitWarehouseList: state => {
      return state.allWarehouseList ? state.allWarehouseList.filter(it => ["TRANSIT"].includes(it.type)) : []
    },


  },
  mutations: {
    setNotify(state, data) {
      state.message_notify_unread = data.unread
      state.message_notify_list = data.notifyList || []
    },

    stopRoute(state, status) {

      state.stopRoute = !!status
    },
    routerFrom(state, from) {

      state.prevRoute = from
    },
    setModifyLayout(store, type) {
      localStorage.setItem('modifyLayout', type)
      store.modifyLayout = type
    },
    changeI18nLocale(state, config) {
      const locale = config.locale
      state.i18nlocale = locale
      if (locale) {
        if (config.localStorage != false) localStorage.setItem("i18nlocale", locale);

        Vue.prototype.$i18nEl.locale = locale
        Vue.prototype.$ex_ajax_extra_header = Object.assign({}, Vue.prototype.$ex_ajax_extra_header, { 'Content-Language': locale })
      } else if (Vue.prototype.$ex_ajax_extra_header) {
        localStorage.removeItem("i18nlocale");
        delete Vue.prototype.$ex_ajax_extra_header['Content-Language']
        Vue.prototype.$i18nEl.locale = 'zh'
        state.i18nlocale = 'zh'
      }
    },


    setAllWarehouse(state, warehouses) {
      let map = {}
      warehouses.forEach(it => {
        map[it.id] = it
      })
      state.allWarehouseMap = map
      state.allWarehouseList = warehouses
    },
  },
  actions: {
    refreshNotice({ commit, }, call) {
      Vue.prototype.$ajax({
        url: "/oms-api/client/OmsBaseNotifyClientRpc/getNotify.json",
        success: (res) => {
          if (res.data) {
            commit("setNotify", res.data)
            if (call) call(res.data)
          }
        },
        fail: () => {
          commit("setNotify", {
            unread: 0,
            notifyList: [],
          })
        },
      })
    },

    setAllWarehouseAction({ commit, getters }) {
      if (Vue.prototype.$loginUser.isCustomerSystem) {
        Vue.prototype.$ajax({
          url: "/oms-api/client/OmsBaseWarehouseClientRpc/selectAllEnable.json",
          success: (res) => {
            if (res && res.data && res.data.rows) {
              commit('setAllWarehouse', res.data.rows)
            }
          }
        })
      }
    },

    resumeI18nLocale({ commit }) {
      let locale = localStorage.getItem("i18nlocale");
      if (locale) {
        commit('changeI18nLocale', { locale })
      } else {
        const browserLang = navigator.language ? navigator.language : navigator.browserLanguage;

        let locale = 'zh'
        if (_.startsWith(browserLang.toLowerCase(), 'en')) {
          locale = 'en'
        } else if (_.startsWith(browserLang.toLowerCase(), 'ru')) {
          locale = 'ru'
        }
        commit('changeI18nLocale', { locale, localStorage: false })
      }
    },

  },
})

export default store
